package com.mac.Artilces;

import com.mac.pojo.ArticleView;
import com.mac.pojo.Articles;
import com.mac.pojo.Member;
import com.mac.pojo.User;
import com.mac.service.ArticlesService;
import com.mac.service.MemberService;
import com.mac.service.UserService;
import org.junit.Test;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;

public class test {
    MemberService memberService = new MemberService();

    UserService userService = new UserService();

    ArticlesService articlesService = new ArticlesService();

    @Test
    public void addArticle(){
        final User user = userService.ifUserExists("Sunflower");

        final Member member = memberService.selectByUserId(user.getUserId());

        Articles article = new Articles();

        article.setTitle("JavaWeb常见报错");
        article.setContent("*****");
        article.setAuthorId(member.getMemberId());
        article.setPublishDate(Timestamp.valueOf(LocalDateTime.now()));
        article.setStatus("published");

        final boolean addArticle = articlesService.addArticle(article);

        System.out.println(addArticle);
    }

    @Test
    public void selectByTitle(){
        final Articles article = articlesService.searchOneByTitle("JavaWeb常见报错");
        System.out.println(article);
    }

    @Test
    public void selectAllArticles(){
        ArrayList<Articles> articles = articlesService.selectAllArticles();

        ArrayList<ArticleView> articleViews = new ArrayList<>();

        for (Articles article : articles) {
            ArticleView articleView = new ArticleView();

            Member member = memberService.selectByUserId(article.getAuthorId());

            articleView.setTitle(article.getTitle());
            articleView.setContent(article.getContent());
            articleView.setPublishDate(article.getPublishDate());
            articleView.setAuthor(member.getMemberName());

            articleViews.add(articleView);

            System.out.println(articleViews);
        }
    }

}
